package com.app.common.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.app.common.dataDictionary.DE.DE_FILE_AVAILABLE;
import com.app.common.dataDictionary.DE.DE_FILE_IMGDOMAIN;
import com.app.common.dataDictionary.DE.DE_FILE_IMGID;
import com.app.common.dataDictionary.DE.DE_FILE_IMGNAME;
import com.app.common.dataDictionary.DE.DE_FILE_IMGPATH;
import com.app.common.dataDictionary.DE.DE_FILE_OPPTIME;
import com.app.common.dataDictionary.DE.DE_FILE_SFSHOW;
import com.app.common.dataDictionary.PO.TFilePO;
import com.css.sword.kernel.base.exception.SwordBaseCheckedException;
import com.css.sword.kernel.utils.SwordFileUtils;
import com.css.sword.platform.web.comm.WebRootUtil;
import com.css.sword.platform.web.fileupload.intimeup.SwordFileItem;

/**
 * 
 * 上传附件
 * @author Caesar
 * 
 */
public class FileUploadUtil {
	
	public static String filePath="FileUpload/UploadImg/";
	/**
	 * 根据传的文件路径，保存上传文件
	 * @param uploadFiles
	 * @param uploadPath
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map uploadFile(List<SwordFileItem> uploadFiles,String subfilepath) throws Exception{
		String datefilepath = "/"+DateFormatUtil.getCurrentYear()+"/"+DateFormatUtil.getCurrentMonth()+"/"+DateFormatUtil.getCurrentDate()+"/";		
		String WebRootPath=WebRootUtil.getWebRootPath();
		//String path = SwordFileUtils.getSwordRootPath();
		String uploadPath=filePath+subfilepath+datefilepath;
		String realUploadPath=WebRootPath+"/"+uploadPath;
		File desPath =new File(realUploadPath);
		//如果文件夹不存在则创建
		if  (!desPath.exists())      
		{
			desPath.mkdirs();//.mkdir();
		}
		Map hashMap = new HashMap();
		String fileId="";
		String fileName="";
		if (uploadFiles != null && uploadFiles.size() > 0) {
			for (SwordFileItem fileItem : uploadFiles) {
				if(fileItem.isNewFile()){
					fileName = fileItem.getName();
					String[] spiltfilename = fileName.split("\\.");
					String fileNameSuffix=spiltfilename[spiltfilename.length-1];
					fileId = fileItem.getFileId()+"."+fileNameSuffix;
					//fileId=System.currentTimeMillis()+"."+fileNameSuffix;
					File file = new File(realUploadPath + fileId);
	                fileItem.write(file);
	                hashMap.put("fileId", fileId);
	                hashMap.put("fileName", fileName);
	                hashMap.put("filePath", uploadPath + fileId);
				}
			}
		}
		return hashMap;
	}

	
	/**
	 * 根据传的文件路径，保存上传文件并且保存文件PO
	 * @param uploadFiles
	 * @param uploadPath
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List uploadFileAndSaveFile(List<SwordFileItem> uploadFiles,String subfilepath) throws Exception{
		String datefilepath = "/"+DateFormatUtil.getCurrentYear()+"/"+DateFormatUtil.getCurrentMonth()+"/"+DateFormatUtil.getCurrentDate()+"/";		
		String WebRootPath=WebRootUtil.getWebRootPath();
		//String path = SwordFileUtils.getSwordRootPath();
		String uploadPath=filePath+subfilepath+datefilepath;
		String realUploadPath=WebRootPath+"/"+uploadPath;
		File desPath =new File(realUploadPath);
		//如果文件夹不存在则创建
		if  (!desPath.exists())      
		{
			desPath.mkdirs();//.mkdir();
		}
		List list = new ArrayList();
		String fileId="";
		String fileName="";
		if (uploadFiles != null && uploadFiles.size() > 0) {
			for (SwordFileItem fileItem : uploadFiles) {
				TFilePO tFilePO = new TFilePO();
				if(fileItem.isNewFile()){
					fileName = fileItem.getName();
					String[] spiltfilename = fileName.split("\\.");
					String fileNameSuffix=spiltfilename[spiltfilename.length-1];
					fileId = fileItem.getFileId()+"."+fileNameSuffix;
					File file = new File(realUploadPath + fileId);
	                fileItem.write(file);
	                tFilePO.setFileImgid(new DE_FILE_IMGID(fileId));
	                tFilePO.setFileImgname(new DE_FILE_IMGNAME(fileName));
	                tFilePO.setFileImgpath(new DE_FILE_IMGPATH(uploadPath+fileId));
	                tFilePO.setFileImgdomain(new DE_FILE_IMGDOMAIN(WebRootUtil.getWebRootPath()));
	                tFilePO.setFileAvailable(new DE_FILE_AVAILABLE("1"));
	                tFilePO.setFileSfshow(new DE_FILE_SFSHOW("1"));
	                tFilePO.setFileOpptime(new DE_FILE_OPPTIME(new Date()));
	                list.add(tFilePO);
				}
			}
		}
		return list;
	}
	
	/**
	 * 获取当前应用系统路径+文件路径
	 * @param filePath
	 * @return
	 * @throws SwordBaseCheckedException
	 */
	public static String getFilePath(String filePath) throws SwordBaseCheckedException{
		String rootPath = getRootPath();
		String uploadPath = rootPath+filePath;
		return uploadPath;
	}
	
	/**
	 * 获取当前应用系统路径
	 * @param filePath
	 * @return
	 * @throws SwordBaseCheckedException
	 */
	public static String getRootPath() throws SwordBaseCheckedException{
		String path = SwordFileUtils.getSwordRootPath();
		int lastIndex = path.lastIndexOf("WEB-INF");
		String rootPath = path.substring(0,lastIndex).replace("\\", "/");
		return rootPath;
	}

}
